package modules.pathloss;

import com.vividsolutions.jts.geom.Geometry;

import java.util.*;

import modules.dimensioning.CoverageInterface;
import datatypes.Antenna;

import javax.naming.directory.InvalidAttributesException;
import tools.settings.SettingsAdapter;

/**
 * <p>
 * Singleton class implementing interface from "dimensioning" module.
 * Adapter class for this module.
 * </p>
 *
 * @author Lynne Vandousselaere
 */
public class CoverageAdapter implements CoverageInterface {

    // Static singleton object of this class.
    private static CoverageAdapter ca;

    // Map containing all MAPL's.
    private Map<String, MAPL> mapl = new HashMap<String, MAPL>();
    private SettingsAdapter sa = SettingsAdapter.getInstance();

    /**
     * <p>
     * Returns the singleton instance of this class.
     * </p>
     *
     * @return the singleton instance of "CoverageAdapter"
     */
    public static CoverageAdapter getInstance() {
        if (ca == null)
            ca = new CoverageAdapter();
        return ca;
    }
    private CoverageAdapter(){}

    public void clear() {
        mapl.clear();
    }

    @Override
    public Geometry getCoverage(Antenna a) throws InvalidAttributesException {
        return getMAPL(a.getType().toString()).getCoverage(a, sa.getSettings());
    }

    @Override
    public double getRangeRadius(Antenna a) throws InvalidAttributesException {
        return getMAPL(a.getType().toString()).getRangeRadius(a, sa.getSettings());
    }

    /** *****************
     *   private methods
     *  *****************/
    /**
     * <p>
     * Returns the MAPL, for the given type.
     * </p>
     *
     * @param type
     * @return an instance of "MAPL"
     */
    private MAPL getMAPL(String type) {
        MAPL node = mapl.get(type);
        if (node == null) {
            // DEFAULT 
            node = new NewOdMAPL();
            if (type.equalsIgnoreCase("omnidirectional"))
                node = new NewOdMAPL();
            // else // add more mapl's here.
        }

        return node;
    }
}